/**
 * 
 */
package jvs.vfs.opt.app;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

/**
 * @author qiangli
 * 
 */
final class Loader {

	private static final int MAX_RECENT_TASKS = 8;

	private static ApplicationInfo getApplicationInfo(PackageManager manager,
			Intent intent) {
		final ResolveInfo ri = manager.resolveActivity(intent, 0);
		if (ri == null) {
			return null;
		}
		final ApplicationInfo info = new ApplicationInfo();
		final ActivityInfo activityInfo = ri.activityInfo;
		info.icon = activityInfo.loadIcon(manager);
		if (info.title == null || info.title.length() == 0) {
			info.title = activityInfo.loadLabel(manager).toString();
		}
		if (info.title == null) {
			info.title = "";
		}
		return info;
	}

	public static void loadApplications(Context ctx,
			ArrayList<ApplicationInfo> list) {
		PackageManager pm = ctx.getPackageManager();
		Intent mi = new Intent(Intent.ACTION_MAIN, null);
		mi.addCategory(Intent.CATEGORY_LAUNCHER);
		final List<ResolveInfo> apps = pm.queryIntentActivities(
				mi, 0);
		Collections.sort(apps, new ResolveInfo.DisplayNameComparator(pm));
		if (apps != null) {
			final int count = apps.size();
			list.clear();
			//
			for (int i = 0; i < count; i++) {
				ApplicationInfo info = new ApplicationInfo();
				ResolveInfo ri = apps.get(i);
				info.title = ri.loadLabel(pm).toString();
				ComponentName cn = new ComponentName(
						ri.activityInfo.applicationInfo.packageName,
						ri.activityInfo.name);
				info.setActivity(cn, Intent.FLAG_ACTIVITY_NEW_TASK
						| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
				//info.icon = ri.activityInfo.loadIcon(pm);
				
				info.icon = null;
				try {
					info.id = makeId(cn);
				
					list.add(info);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void loadRecents(Context ctx, ArrayList<ApplicationInfo> list) {
		final PackageManager manager = ctx.getPackageManager();
		final ActivityManager tasksManager = (ActivityManager) ctx
				.getSystemService(Activity.ACTIVITY_SERVICE);
		final List<ActivityManager.RecentTaskInfo> recentTasks = tasksManager
				.getRecentTasks(MAX_RECENT_TASKS, 0);

		final int count = recentTasks.size();
		list.clear();
		//
		for (int i = 0; i < count; i++) {
			final Intent intent = recentTasks.get(i).baseIntent;
			if (Intent.ACTION_MAIN.equals(intent.getAction())
					&& !intent.hasCategory(Intent.CATEGORY_HOME)) {
				ApplicationInfo info = getApplicationInfo(manager, intent);
				if (info != null) {
					info.intent = intent;
					info.type = Types.RECENT;
					try {
						info.id = makeId(intent.getComponent());
						
						list.add(info);
					} catch (Exception e) {
						e.printStackTrace();
					}
					
				}
			}
		}
	}

	private static String makeId(ComponentName c) throws Exception {
		//String n = c.toString().replaceAll("/", "_");
		//return FileSystem.encodePath(n);
		//String n = c.getPackageName()+":"+c.getClassName();
		//return URLEncoder.encode(n, "UTF-8");
		return c.getPackageName();
	}

	private Loader() {
	}
}
